package com.docmanage.am.service.impl;

import java.util.*;

import cn.hutool.core.collection.CollUtil;
import com.docmanage.am.domain.vo.FileDirectoryTreeVo;
import com.docmanage.common.utils.DateUtils;
import com.docmanage.common.utils.SecurityUtils;
import org.springframework.stereotype.Service;
import com.docmanage.am.mapper.AmFileDirectoryMapper;
import com.docmanage.am.domain.AmFileDirectory;

import javax.annotation.Resource;

/**
 * 目录管理Service业务层处理
 * 
 * @author docmanage
 * @date 2024-03-07
 */
@Service
public class AmFileDirectoryServiceImpl
{
    @Resource
    private AmFileDirectoryMapper amFileDirectoryMapper;

    /**
     * 查询目录管理
     * 
     * @param id 目录管理主键
     * @return 目录管理
     */
    public AmFileDirectory selectAmFileDirectoryById(Long id)
    {
        return amFileDirectoryMapper.selectAmFileDirectoryById(id);
    }

    /**
     * 查询目录管理列表
     * 
     * @param amFileDirectory 目录管理
     * @return 目录管理
     */
    public List<AmFileDirectory> selectAmFileDirectoryList(AmFileDirectory amFileDirectory)
    {
        return amFileDirectoryMapper.selectAmFileDirectoryList(amFileDirectory);
    }

    /**
     * 根据角色获取树结构
     * @param parentId
     * @return
     */
    public List<FileDirectoryTreeVo> selectAmFileDirectoryTree(Long parentId){
        List<Long> roleIds = SecurityUtils.getRoleId();
        //获取目录
        List<FileDirectoryTreeVo> list = amFileDirectoryMapper.selectAmFileDirectoryTree(parentId, roleIds);

        list.forEach(item->{
          item.setChildren(amFileDirectoryMapper.selectAmFileDirectoryTree(item.getId(),roleIds));
        });
        return list;
    }

    /**
     * 新增目录管理
     * 
     * @param amFileDirectory 目录管理
     * @return 结果
     */

    public int insertAmFileDirectory(AmFileDirectory amFileDirectory)
    {
        if (Objects.isNull(amFileDirectory.getParentId())){
            //设置父id
            amFileDirectory.setParentId(0L);
        }
        amFileDirectory.setCreateBy(SecurityUtils.getUsername());
        amFileDirectory.setUpdateBy(SecurityUtils.getUsername());
        return amFileDirectoryMapper.insertAmFileDirectory(amFileDirectory);
    }

    /**
     * 修改目录管理
     * 
     * @param amFileDirectory 目录管理
     * @return 结果
     */

    public int updateAmFileDirectory(AmFileDirectory amFileDirectory)
    {
        amFileDirectory.setUpdateTime(DateUtils.getNowDate());
        amFileDirectory.setUpdateBy(SecurityUtils.getUsername());
        return amFileDirectoryMapper.updateAmFileDirectory(amFileDirectory);
    }

    /**
     * 批量删除目录管理
     * 
     * @param ids 需要删除的目录管理主键
     * @return 结果
     */

    public int deleteAmFileDirectoryByIds(Long[] ids)
    {
        return amFileDirectoryMapper.deleteAmFileDirectoryByIds(ids);
    }

    /**
     * 删除目录管理信息
     * 
     * @param id 目录管理主键
     * @return 结果
     */
    public int deleteAmFileDirectoryById(Long id)
    {
        return amFileDirectoryMapper.deleteAmFileDirectoryById(id);
    }

    public static List<FileDirectoryTreeVo> listToTree(List<FileDirectoryTreeVo> nodes, Long parentId) {
        List<FileDirectoryTreeVo> result = new ArrayList<>();
        for (FileDirectoryTreeVo node : nodes) {
            if (node.getParentId().equals(parentId)) {
                FileDirectoryTreeVo treeNode = new FileDirectoryTreeVo(node.getId(), node.getDirectory(), node.getParentId());
                result.add(treeNode);
                treeNode.setChildren(listToTree(nodes, node.getId())); // 递归调用
            }
        }
        return result;
    }

    public List<FileDirectoryTreeVo> AmFileDirectoryTree(long parentId, String directory) {
        List<AmFileDirectory> list = amFileDirectoryMapper.selectAmFileDirectoryList(new AmFileDirectory());
        List<FileDirectoryTreeVo> treeVos = new ArrayList<>();
        list.forEach(item -> {
            treeVos.add(new FileDirectoryTreeVo(item.getId(), item.getDirectory(), item.getParentId()));
        });
        return listToTree(treeVos, 0L);
    }



}
